
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ExonEnum  {
	
	
	private File newFile;
	private File currentFile;
	private Match match = Match.None;
	private boolean matchPart;
	private int matchLine;
	private int startIndex;
	private int endIndex;
	private enum Match {DateAndSize, DateOrSize, Date, Size,None}
	protected final int[] SUCCESS = {1};
	protected final int[] FAIL = {0};
	
	public static void main(String[] args) {
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("file1", "C:\\FOFDB.kz");
		map.put("file2", "C:\\RK\\FOFDB.kz");
		map.put("matchPart", "1,65,85");
		ExonEnum exEnum = new ExonEnum();
		boolean flag = exEnum.InitializeTask(map);
		if(flag){
			int i[] = exEnum._ExecuteTask();
			System.out.println(i[0]);
		}
		
	}
	
	
	
	
	
	
	public boolean InitializeTask(Map<String, String> properties){

	    if (properties.containsKey("file1"))
	    {
	    	String fileName = properties.get("file1");
	    	newFile = new File(fileName);
	    }
		else{
			}
		
		if (properties.containsKey("file2"))
		{
			String fileName =properties.get("file2");
			currentFile = new File(fileName); 
		}
		else{
		}
		
		if (properties.containsKey("DateAndSize"))
		{
			String matchTemp = properties.get("DateAndSize");
			if(matchTemp.matches("DateAndSize"))
				match = Match.DateAndSize;
			else if(matchTemp.matches("Date"))
				match = Match.Date;
			else if(matchTemp.matches("Size"))
				match = Match.Size;
			else{
			}
		}
		if (properties.containsKey("matchPart"))
		{
			String matchPartStr = properties.get("matchPart");
			String[] split = matchPartStr.split(",");
			if (split.length==3)
			{
				try
				{
					matchLine = Integer.parseInt(split[0]);
					startIndex = Integer.parseInt(split[1]) - 1;
					endIndex = Integer.parseInt(split[2]) - 1;
				}
				catch(NumberFormatException e)
				{
					e.printStackTrace();
				}
				matchPart = true;
			}
			else
			{
			}
		}
			
		return true;
	}

	public int[] _ExecuteTask() {
		if(!newFile.exists())
		{
			return FAIL;
		}
		if(!currentFile.exists())
		{
			return FAIL;
		}
		
		if(match == Match.DateOrSize)
		{
			if (matchDate(newFile, currentFile) || matchSize(newFile, currentFile))
				return FAIL;
		}
		else if(match == Match.DateAndSize)
		{
			if (matchDate(newFile, currentFile) && matchSize(newFile, currentFile))
				return FAIL;
		}
		else if(match == Match.Date)
		{
			if (matchDate(newFile, currentFile))
				return FAIL;
		}
		else if(match == Match.Size)
		{
			if (matchSize(newFile, currentFile))
				return FAIL;
		}
		
		if(matchPart && matchPart(newFile,currentFile))
			return FAIL;
		
		return SUCCESS;
	}

	private boolean matchDate(File file1,File file2)
	{
		if(file1.lastModified() == file2.lastModified())
			return true;
		else 
			return false;		
	}
	
	private boolean matchSize(File file1,File file2)
	{
		if(file1.getUsableSpace() == file2.getUsableSpace())
			return true;
		else 
			return false;		
	}
	
	private boolean matchPart(File file1, File file2)
	{
		String file1part = getPart(file1);
		String file2part = getPart(file2);

		if(file1part == null || file2part == null || (file1part.compareTo(file2part) > 0) )//!file1part.equals(file2part)
			return false;
		return true;
	}
	
	private String getPart(File file)
	{
		BufferedReader br;
		try {
			br = new BufferedReader(new FileReader(file));
	        String line = br.readLine();
	        int i = 1;
	        
	        while (line != null) {
	        	if(i == matchLine) 
        		{
	        		br.close();
	        		//start
	        		if(endIndex > line.length()){
	    				endIndex = line.length();
	    			}
	        		//end
	        		return (endIndex==0)?line.substring(startIndex):line.substring(startIndex,endIndex);
        		}
	        	i++;
	            line = br.readLine();
	        }
	        br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
